from django.shortcuts import render
from django.http import HttpResponse,JsonResponse
from myadmin import models
from django.contrib.auth.hashers import make_password,check_password

import random,requests
# Create your views here.

#登陆页面
def login(request):

    return render(request, 'myhome/login/login.html')

#执行登陆页面
def dologin(request):
    data=request.POST.dict()
    data.pop('csrfmiddlewaretoken')

    nextpath=request.GET.get('nextpath')
    if nextpath == '':
        nextpath = '/'

    # 通过手机号获取用户对象
    # 检测账户和密码
    obj = models.Users.objects.filter(phone=data['phone'])
    if obj.count() == 0:
        return HttpResponse('<script>alert("账号不存在");history.back();</script>')

    # 验证密码
    from django.contrib.auth.hashers import make_password, check_password
    if check_password(data['password'], obj[0].password):
        # 完成登陆操作
        request.session['VipUser'] = {'id': obj[0].id, 'phone': obj[0].phone}
        return HttpResponse(f'<script>alert("欢迎登陆");location.href="{nextpath}";</script>')
    else:
        return HttpResponse('<script>alert("密码不正确");history.back();</script>')

#退出页面
def logout(request):
    request.session.flush()
    return HttpResponse('<script>alert("退出成功");location.href="/login"</script>')

#注册界面
def register(request):
    return render(request, 'myhome/login/register.html')

#执行注册操作
def doregister(request):
    data={}
    data['phone']=request.POST.get('phone')
    data['password']=request.POST.get('password')
    # 验证手机验证码是否正确

    #密码加密
    data['password']=make_password(data['password'],None,'pbkdf2_sha256')

    # 存入数据库
    obj = models.Users(**data)
    obj.save()

    return HttpResponse('<script>alert("注册成功，欢迎登录");location.href="/login"</script>')


#手机号注册时的验证
def checkphone(request):
    #接收手机号码
    phone=request.GET.get('phone')
    #验证
    n=models.Users.objects.filter(phone=phone).count()
    if n:
        return JsonResponse({'code':1,'msg':'手机号码已存在'})
    else:
        return JsonResponse({'code':0,'msg':'手机号码可用'})

#给手机号码发送短信验证
def sendphone(request):
    # 接收手机号码
    phone=request.GET.get('phone')

    # 发送短信验证的api地址
    vcode=random.randint(10000,99999)
    request.session['vcode']={'phone':phone,'vcode':vcode}
    if SendCode(phone,vcode):
        return JsonResponse({'code':0,'msg':'短信验证码已经发送，请查收','vcode':vcode})
    else:
        return JsonResponse({'code':1,'msg':'短信验证码发送失败','vcode':vcode})

#请求短信验证
def SendCode(phone,vcode):
    url='https://open.ucpaas.com/ol/sms/sendsms'
    # 请求时的post数据
    data={
        "sid":"4c0064b5f77af9da37fb74145594092e",
        "token":"1db32087f3b4111610f753a4ded823c1",
        "appid":"328129a7423b4bf595bc4435e80f8e1c",
        "templateid":"510851",
        "param":vcode,
        "mobile":phone
    }
    headers={
        'Content-Type':'application/json'
    }

    #发送请求
    response=requests.post(url,json=data,headers=headers)
    #接收响应结果
    res=response.json()
    print(res)
    #判断当前短信是否发送成功
    if res['code'] == '000000' and res['msg'] == 'OK':
        return True
    else:
        return False
